<template>
    <div>
        <Card>
            <Row class="form-row">
                <Form :model="filters" label-position="right" :label-width="90" inline ref="formInline">
                    <FormItem label="小区">
                        <div class="filters-input">
                            <el-select v-model="filters.name" placeholder="请选择小区" filterable
                                       clearable no-data-text="暂无数据">
                                <el-option v-for="item in houseList" :value="item.name" :key="item.name"
                                           :label="item.name"></el-option>
                            </el-select>
                        </div>
                    </FormItem>
                    <FormItem>
                        <Button type="primary" icon="ios-search" @click="handleQuery('formInline')">搜索</Button>
                    </FormItem>
                </Form>
            </Row>
            <el-table stripe border :loading="tableLoading" :data="tableData" ref="table" style="width: 100%">
                <template slot="empty">暂无数据</template>
                <el-table-column label="小区名称">
                    <template slot-scope="scope">
                        {{scope.row.name}}
                        <!--{{housingNameList[scope.row.id]}}-->
                    </template>
                </el-table-column>
                <el-table-column prop="shhAppId" label="生活号APPID"></el-table-column>
                <el-table-column prop="alipayToken" label="支付宝授权token"></el-table-column>
                <el-table-column prop="alipayAccount" label="支付宝收款账号"></el-table-column>
                <el-table-column label="操作" width="150" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" @click="HandleEdit( scope.row )">配置</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <Page style="margin: 10px 0;" :total="pageTotal" :current="pageNum" :page-size="pageSize" show-total
                  id="lyPage"
                  show-sizer show-elevator @on-change="handlePage" @on-page-size-change='handlePageSize'>
            </Page>

        </Card>


        <Modal v-model="modalEdit" title="修改" width="800">
            <Form ref="saveHousing" :model="Housing" :rules="HousingRules" :label-width="110">
                <FormItem label="小区名称" prop="name">
                    <Input v-model="Housing.name" disabled placeholder="小区名称" class="normal-input"></Input>
                </FormItem>

                <FormItem label="生活号APPID" prop="shhAppId">
                    <Input v-model="Housing.shhAppId" clearable placeholder="生活号APPID" class="normal-input"></Input>
                </FormItem>

                <FormItem label="支付宝授权token" prop="alipayToken">
                    <Input v-model="Housing.alipayToken" clearable placeholder="支付宝授权token"
                           class="normal-input"></Input>
                </FormItem>

                <FormItem label="支付宝收款账号" prop="alipayAccount">
                    <Input v-model="Housing.alipayAccount" clearable placeholder="支付宝收款账号" class="normal-input"></Input>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" size="large" @click="onCancel()">关闭</Button>
                <Button type="primary" size="large" @click="handleSubmit" :loading="tableLoading">确定</Button>
            </div>
        </Modal>

    </div>
</template>


<script>
    //线上下载模板地址
    import baseURL from "_conf/url";
    import Treeselect from "@riophae/vue-treeselect";
    import Cookies from "js-cookie";
    import {TOKEN_KEY} from "@/libs/util";
    import moment from "moment"
    //线上上传账单地址
    const act = baseURL + "/api/bill/upload";

    export default {
        data() {
            return {
                //查询条件
                filters: {},
                //页码
                pageNum: 1,
                //每页数量
                pageSize: 10,
                //总数
                pageTotal: 0,
                tableData: [],
                houseList: [],
                //表单提交状态
                modal: {},
                tableLoading: false,
                modalEdit: false,
                Housing: {},
                HousingRules: {
                    name: [
                        {required: true, message: "请选择小区"}
                    ],
                },
            }
        },
        created: function () {
            this.loadData();
            this.gethouse();
        },
        methods: {
            //加载数据
            loadData: function () {
                if (this.tableLoading) return;
                this.tableLoading = true;
                let params = this.filters;
                params.pageNum = this.pageNum;
                params.pageSize = this.pageSize;

                // console.log(params)

                //与后台交换数据
                this.postRequest("/api/housing/loadData", params).then(res => {
                    console.log(res);
                    this.tableLoading = false;
                    if (res.code == 20000) {
                        this.tableData = res.data.list;
                        this.pageTotal = res.data.total;
                    } else {
                        this.tableData = [];
                        this.$Message.error({
                            content: res.message,
                            duration: 5
                        });
                    }
                });
            },
            //加载小区
            gethouse() {
                let url = "/api/housing/getHousingByLogin";
                this.postRequest(url).then(res => {
                    this.houseList = res;
                });
            },
            //查询
            handleQuery(name) {
                this.loadData();
            },
            handlePage(value) {
                this.pageNum = value;
                this.loadData();
            },
            handlePageSize(value) {
                this.pageSize = value;
                this.loadData();
            },
            onCancel: function () {
                this.modalEdit = false;
            },
            //    编辑按钮
            HandleEdit: function (row) {
                this.modalEdit = true
                this.Housing = row;
            },
            //确认按钮
            handleSubmit: function () {
                this.$refs['saveHousing'].validate(valid => {
                    if (valid) {
                        const url = "/api/housing/saveConfigKey";
                        console.log(this.Housing);
                        this.postRequest(url, this.Housing).then(res => {
                            this.tableLoading = false
                            if (res.code == 20000) {
                                this.$Message.success("操作成功！")
                                this.modalEdit = false
                                this.loadData()
                                return
                            }
                            this.$Message.error(res.message)
                        })
                    }
                })
            },
        }
    };
</script>
